//
//  searchAllMacViewController.m
//  MMWMiotLinkDemo
//
//  Created by   HGG  on 16/1/12.
//  Copyright © 2016年 HGG. All rights reserved.
//

#import "searchAllMacViewController.h"
#import "MMWMiotLink.h"
#import "vspOperation.h"
#import "dataSend-receiveViewController.h"

#import "configRouterViewController.h"

@interface searchAllMacViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *searchTimeTF;
@property (weak, nonatomic) IBOutlet UITableView *searchResultTableView;
@property (strong,nonatomic) NSArray *allMacAndIP;

@end

@implementation searchAllMacViewController
- (IBAction)btnClick:(UIBarButtonItem *)sender {
    
    // 获取指定的Storyboard，name填写Storyboard的文件名
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        // 从Storyboard上按照identifier获取指定的界面（VC），identifier必须是唯一的
    configRouterViewController *receive = [storyboard instantiateViewControllerWithIdentifier:@"configRouterID"];
       
    [self.navigationController pushViewController:receive animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _searchResultTableView.hidden = YES;
    //_searchResultTableView.tableFooterView.hidden = YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [_searchTimeTF resignFirstResponder];
}
- (IBAction)searchAllMacAndIPAction:(id)sender {
    _searchResultTableView.hidden = YES;
    [[MMWMiotLink shareInstence]mmw_MiotLinkGetAllMacAndIPSearchTime:_searchTimeTF.text.intValue callBack:^(NSArray *allMacAndIP) {
        self.allMacAndIP = [[NSArray alloc]initWithArray:allMacAndIP];
        [_searchResultTableView reloadData];
        _searchResultTableView.hidden = NO;
        for (NSDictionary *dic in allMacAndIP) {
            NSLog(@"%@--",dic);
        }
      
    }];
    

    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _allMacAndIP.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.text = [NSString stringWithFormat:@"%@_%@_%@",_allMacAndIP[indexPath.row][@"Mac"],_allMacAndIP[indexPath.row][@"IP"],_allMacAndIP[indexPath.row][@"Serial"]];
    
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *deviceInfo = _allMacAndIP[indexPath.row];
    __weak typeof(self)weakSelf = self;
    [[vspOperation shareInstance] activedisconnect];
    [[vspOperation shareInstance]connectDeviceGetIp:deviceInfo[@"IP"] port:[deviceInfo[@"Serial"]intValue] success:^{
        NSLog(@"step_two:连接Socket成功");
        __strong typeof(weakSelf)strongSelf = weakSelf;
        dispatch_async(dispatch_get_main_queue(), ^{
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            dataSend_receiveViewController *dataSend_receiveViewVC = [storyboard instantiateViewControllerWithIdentifier:@"dataSRVc"];
            
            [strongSelf.navigationController pushViewController:dataSend_receiveViewVC animated:YES];
        });
        
    } failure:^(NSError *error) {
        NSLog(@"");
        
    }];

    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
